from itertools import zip_longest
from urllib.parse import unquote

from django.shortcuts import render
from django.views import View
from direction import models as direction_models
from home import models as home_models


# Create your views here.

class StudyDirectionView(View):
    def get(self, request):
        return render(request, 'direction.html')

class DirectionSelectCourseView(View):
    def get(self, request, targetName):
        TargetName = unquote(targetName)
        resource_queryset = home_models.Resource.objects.filter(resource_type='video', resource_tag__name=TargetName)
        direction_queryset = direction_models.Direction.objects.all()
        directionCourse_queryset = direction_models.DirectionCourse.objects.all()

        # 计算QuerySet长度以确定需要多少组
        group_size = 3

        # 使用zip_longest进行分组，fillvalue参数用于填充不足的组
        groups = list(zip_longest(*[iter(resource_queryset)] * group_size, fillvalue=None))

        # 将分组数据传递给模板
        context = {'valid_groups': groups, 'execute_function': True, 'targetName': TargetName,
                   'Direction': direction_queryset, 'DirectionCourse': directionCourse_queryset}
        return render(request, 'Course.html', context)


